package com.supermap.dataservice.mysql.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.supermap.dataservice.mysql.dao.DataMapper;
import com.supermap.dataservice.mysql.service.UserService;
import com.supermap.dataservice.mysql.dao.UserMapper;
import com.supermap.dubbo.service.domain.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private DataMapper dataMapper;

    @Override
    public int addUser(String name, String age) {
        return userMapper.addUser(name, age);
    }

    @Override
    public User findById(String id) {
        String sql ="select * from user where id ='" + id + "'";
        List<Map> data = dataMapper.select(sql);
        JSONObject userJson = JSON.parseArray(JSONObject.toJSONString(data)).getJSONObject(0);
        User user = JSON.toJavaObject(userJson,User.class);
        return user;
    }

    @Override
    public void updataById(String id, String name) {
        userMapper.updataById(id, name);
    }

    @Override
    public void deleteById(String id) {
        userMapper.deleteById(id);
    }
}
